package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.pojo.controlspec.spinner.SpinnerSpec;

public class SpinnerParser {
	public static final String ADDBLANKROW = "addblank";
	public static final String CONTENTDATA = "contentdata";
	public static final String DISPLAYPROP = "displayprop";
	public static final String NEW_ROW_ITEM = "newRowItem";
	public static final String ROWITEM = "rowitem";
	public static final String RPTRDESIGN = "rptrdesign";

	public static SpinnerSpec parse(final Node nControlNode) {
		final SpinnerSpec spinnerSpec = new SpinnerSpec();
		if (nControlNode.hasAttributes()) {
			final String isAddBlank = XmlPaserUtil.getAttrValue(nControlNode,
					SpinnerParser.ADDBLANKROW);
			if (!TextUtils.isEmpty(isAddBlank)) {
				if (isAddBlank.equalsIgnoreCase("Y")) {
					spinnerSpec.setAddBlank(true);
				}

			}
			final String displayProperty = XmlPaserUtil.getAttrValue(
					nControlNode, SpinnerParser.DISPLAYPROP);
			if (!TextUtils.isEmpty(displayProperty)) {
				spinnerSpec.setDisplayProperty(displayProperty);
			}

			final String contentData = XmlPaserUtil.getAttrValue(nControlNode,
					SpinnerParser.CONTENTDATA);

			if (!TextUtils.isEmpty(contentData)) {
				spinnerSpec.setContentData(contentData);
			}

			final String rptrDesign = XmlPaserUtil.getAttrValue(nControlNode,
					SpinnerParser.RPTRDESIGN);
			if (!TextUtils.isEmpty(rptrDesign)) {
				spinnerSpec.setRptrdesign(rptrDesign);
			}
			final String rowItem = XmlPaserUtil.getAttrValue(nControlNode,
					SpinnerParser.ROWITEM);
			if (!TextUtils.isEmpty(rowItem)) {
				spinnerSpec.setRowItem(rowItem);
			}
		}
		final NodeList nodeList = ((Element) nControlNode)
				.getElementsByTagName(SpinnerParser.NEW_ROW_ITEM);
		if (nodeList.getLength() == 1) {
			final Node node = nodeList.item(0);
			final String value = XmlPaserUtil.getNodeValue(node);
			spinnerSpec.setNewRowItem(value);
		}
		return spinnerSpec;
	}
}
